{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b7cdb7db-e805-48da-aa03-028712011b06",
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "82171207-9ac4-4b3f-be93-1d646a54d7f5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3024, 4032, 3)\n",
      "(4351, 8305, 3)\n"
     ]
    }
   ],
   "source": [
    "hist_img = cv2.imread('targ2.png')\n",
    "img = cv2.imread('contact.png')\n",
    "\n",
    "print(hist_img.shape)\n",
    "print(img.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e25d0f2d-73e2-4ee8-bd45-d2bc4652240a",
   "metadata": {},
   "outputs": [],
   "source": [
    "hist = cv2.calcHist([hist_img], [0], None, [256], [0, 256])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "0a4b3951-b059-4f6a-91d4-d0adbad0fbcc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 60.  86. 119. 139. 169. 202. 282. 407. 780.]\n",
      "[4.92094978e-06 7.05336151e-06 9.75988405e-06 1.14002005e-05\n",
      " 1.38606756e-05 1.65671972e-05 2.31284648e-05 3.33804419e-05\n",
      " 6.39723512e-05]\n"
     ]
    }
   ],
   "source": [
    "cdf = hist.cumsum()\n",
    "cdf_normalized = cdf / cdf[-1]\n",
    "print(cdf[:9])\n",
    "print(cdf_normalized[:9])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ecb6fdcb-28aa-47eb-b3ab-fef2445300d3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(4351, 8305, 3)\n"
     ]
    }
   ],
   "source": [
    "mapping = np.round(cdf_normalized * 255).astype(np.int32)\n",
    "normalized_image = mapping[img]\n",
    "\n",
    "print(normalized_image.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "ca28b2c8-34b7-4158-9e3a-5c60fbfe67b8",
   "metadata": {},
   "outputs": [],
   "source": [
    "img = cv2.resize(normalized_image.astype(np.uint8), (1920, 1080))\n",
    "cv2.imshow('Normalized Image', img)\n",
    "cv2.imwrite('afterNormalize.png', img)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ddedcb95-fa23-4705-aef8-1690f0860f8a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
